/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RunableClass;

import RunableClass.AlertRunnable;
import Alert.ST_name;
import RunableSt.authenticator;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import STOCK.*;
import java.io.File;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;
import javax.swing.table.DefaultTableModel;
import mysql.connect;
/**
 *
 * @author Anatomy
 */
public class ReadAlertRunnable implements Runnable {
String symbolx;int Block[]=new int[12];
public static String[]sym=new String[12];String x ="";
    private DefaultTableModel model1;

                   
    /**
     * Reads text from a file line by line
     */
    public void readFromFile(String filename,String symbol) throws SQLException {

        
        BufferedReader bufferedReader = null;
        String xc ="";
        String ya;
       
        
        try {
            //Construct the BufferedReader object
            bufferedReader = new BufferedReader(new StringReader(filename));
            String line = null;
          
            while ((line = bufferedReader.readLine()) != null) {
                  int count1 = 0;int b[] = new int[1000];int c[] =new int[1000];int count2 = 0;
                //System.out.println(line.toString());
                            String linenumber = "";
                            String y ="";
                    if(line.contains("C13_FastQuote_Main.jsp?txtSymbol="+symbol+"\"")){
                            for(int i=0;i<5;i++){
                                char xx=line.charAt(i);
                                
                                if(xx>='0'&&xx<='9'){
                                    linenumber+=line.substring(i, i+1);       
                                }        
                            } //System.out.print(linenumber);  //System.out.println(aa);
                       if(line.startsWith(linenumber)){
                        for(int i=0;i<line.length();i++){
                       char a= line.charAt(i);
                      if(a=='>'){
                          ++count1;
                         b[count1]=i;
                      }else if(a=='<'){
                           ++count2;
                          c[count2]=i;
                      }}
                   for(int i=0;i<100;i++){
                          if(b[i]!=0&&c[i]!=0){
                             if(isDigit(line.substring(b[i]+1,c[i]))){
                                 ya=line.substring(b[i]+1,c[i]);
                                 if(ya.equals(symbol)){
                                     sym[0]=ya;
                                     
                                 }
                             }
                        } }
                  //  x+="@"+y;
                    

                    for(int k=0;k<=11;k++){
                        Block[k]=Integer.parseInt(linenumber)+k;
                        
                        
                        
                    }
                     }
                    // System.out.println(Block[1]);
                   
                            ////////////////////////////////////
                    // System.out.print(""+y);
                     } 
                        for(int k=1;k<=11;k++){
                       
                        if(line.startsWith(String.valueOf(Block[k])+"-->")){
                        for(int i=0;i<line.length();i++){
                       char a= line.charAt(i);
                      if(a=='>'){
                          ++count1;
                         b[count1]=i;
                      }else if(a=='<'){
                           ++count2;
                          c[count2]=i;
                      }}
                   for(int i=0;i<100;i++){
                          if(b[i]!=0&&c[i]!=0){
                             if(isDigit(line.substring(b[i]+1,c[i]))){              
                                      xc=line.substring(b[i]+1,c[i]);
                                      
                             }}
                             }

                        if((!xc.equals("XD")||!xc.equals("SP")||!xc.equals("NP")||!xc.equals("SP,NP,NC")||!xc.equals("XW")
                           ||!xc.equals("SP,NC")||!xc.equals("SP,NP")||!xc.equals("NP,NC" ))&& k==1){


                           y="NOTVALUE";
                          
                       }else{
                           y=xc;
                           
                       }
                        
                    //x+="@"+y;
                  sym[k]=y;
                   if(k==11){
                    break;

                           }
                     //  System.out.print(y);
                     }
                        
                   
                    }
                 
 
        
            
         
            }
        }
 catch (FileNotFoundException ex) {
            Logger.getLogger(AlertRunnable.class.getName()).log(Level.SEVERE, null, ex);
        }         catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            //Close the BufferedReader
            try {
                if (bufferedReader != null)
                    bufferedReader.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
 static  StringBuffer     content        =   new StringBuffer(""); 
    /**
     * @param args the command line arguments
     */
  public static void main(String[] args) throws SQLException {
     
         for(;;){ 
                
                  
        try {
          
         ReadAlertRunnable RD=new ReadAlertRunnable();
            RD.readFromFile(content.toString(),"SCC");//STOCK.txtSymbol.getText().toUpperCase());
         
      
             // STOCK.lbSymbol2.setText(STOCK.txtSymbol.getText().toUpperCase());
             // STOCK.lbvalue2.setText(sym[5]);
              System.out.println(ReadAlertRunnable.sym[0]+"-"+ReadAlertRunnable.sym[1]+"-"+ReadAlertRunnable.sym[2]);
       
        } catch (SQLException ex) {
        }
              
              
             
        
      
             
            try {
                Thread.sleep(3500);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
    
         }
  }
    
     public static boolean isDigit(String s)   {
     boolean right = false;
      char c = 0;
      for (int index = 0; index < s.length(); index ++)  {
            c = s.charAt(index);
            if (c >= '0' || c <= '9'||c=='.')  {
            right = true;
           }
       }
      return right;
}

    @Override
    public void run() {
           
          for(;;){ 
                                     //////////////////////////////////สร้างการดึงค่าจาก URL/////////////////////////////////////////////////////////////////////////////**/
     /**/         try {
         
              int count2=1;
         String buffer;
        String url = "http://www.settrade.com/C13_MarketSummaryStockType.jsp?type=S",                                                         /**/                                                      
     /**/          proxy = "proxy.up.ac.th",                                                                                        /**/
     /**/          port = "8080",                                                                                                   /**/
     /**/          username = "up\\51286885",                                                                                       /**/
     /**/          password = "7489250721";                                                                                         /**/
     /**/                                                                                                                           /**/
     /**/       //URL autoIP = new URL("http://checkip.dyndns.org:8245/");                                                          /**/
     /**/       //System.out.println("Get URL");                                                                                    /**/
     /**/                                                                                                                           /**/
     /**/     URL server = new URL(url);                                                                                            /**/
     /**/                                                                                                                           /**/
     /**/       Authenticator.setDefault(new authenticator(                                                                         /**/
     /**/                   username,password));                                                                                    /**/
     /**/       Properties systemProperties = System.getProperties();                                                               /**/
     /**/       systemProperties.setProperty("http.proxyHost",proxy);                                                               /**/
     /**/       systemProperties.setProperty("http.proxyPort",port);                                                                /**/
     /**/       HttpURLConnection connection = (HttpURLConnection)server.openConnection();                                          /**/
     /**/      connection.connect();                                                                                                /**/
     /**/                                                                                                                           /**/
     /**/                                                                                                                           /**/
     /**/       try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"TIS-620"))) {        /**/
     /**/                                                                                                                           /**/
     /**/                                                                                                                                /**/                                                                     /**/
     /**/                content = new StringBuffer("");                                                                                                             /**/
     /**/           while((buffer = in.readLine()) != null) {                                                                     /**/  
     /**/                  count2++;                                                                                              /**/  
     /**/                                                                                                                           /**/  
     /**/                                                                                                                         /**/  
     /**/                  content.append(count2).append("-->").append(buffer).append("\n");                                      /**/
     /**/                                                                                                                         /**/      
      /**/            }                                                                                                           /**/  
      /**/      }                                                                                                                 /**/  
     /**/             } catch (IOException io){
            System.err.println("Couldn't create help URL: " + io);
        

    }   
              
              
              
              
           
                  
        try {
          
          //ReadData RD=new ReadData();
            readFromFile(content.toString(),STOCK.txtSymbol.getText().toUpperCase());
         
      
              STOCK.lbSymbol2.setText(STOCK.txtSymbol.getText().toUpperCase());
              STOCK.lbvalue2.setText(ReadAlertRunnable.sym[5]);
              // System.out.println(this.sym[5]);
       
        } catch (SQLException ex) {
        }
              
              
             
        
      
             
            try {
                Thread.sleep(3500);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
    
    
    }}
   
}
